import Vue from 'vue'
import VueRouter from 'vue-router'
import { Dialog } from 'vant'
import store from '../store'


Vue.use(VueRouter)

const layout = _ => import('../views/layout/index.vue')
const home = _ => import('../views/home/home.vue')
const queans = _ => import('../views/queans/queans.vue')
const video = _ => import('../views/video/video.vue')
const profile = _ => import('../views/profile/profile.vue')
const login = _ => import('../views/login/login.vue')
const search = _ => import('../views/search/search.vue')
const article = _ => import('../views/article/article.vue')
const ffans = _ => import('../views/ffans/ffans.vue')
const colhis = _ => import('../views/colhis/colhis.vue')
const editoruser = _ => import('../views/ediuser/editor-user.vue')
const smartchat = _ => import('../views/chat/smart-chat.vue')
const userdetail = _ => import('../views/userdetail/user-detail.vue')


const routes = [
  { path: '/', redirect: '/home' },
  {
    path: '/', component: layout, children: [
      { path: 'home', component: home },
      { path: 'queans', component: queans },
      { path: 'video', component: video },
      { path: 'profile', component: profile },
    ]
  },
  { path: '/login', component: login, name: "login" },
  { path: '/search', component: search },
  { path: '/article/:articleId', component: article, props: true },
  { path: '/ffans', component: ffans, name: "ffans" },
  { path: '/colhis', component: colhis, name: "colhis", meta: { requiresAuth: true } },
  { path: '/profile/editor', component: editoruser },
  { path: '/chat', component: smartchat, meta: { requiresAuth: true } },
  { path: '/detail/:userId', component: userdetail },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  //如果该页面拥有未登录访问权限
  if (to.name === 'login' || !to.meta.requiresAuth) {
    return next()
  }
  //已经登录
  if (store.state.USER_TOKEN) {
    return next()
  }

  Dialog.confirm({
    title: '该功能需要登录，确认登录吗？'
  }).then(() => {
    next({
      name: 'login',
      query: {
        redirect: from.fullPath
      }
    })
  }).catch(() => {
    // on cancel
  })

})

export default router
